<meta content="text/html; charset=UTF-8; X-Content-Type-Options=nosniff" http-equiv="Content-Type" />
<style type="text/css">
    #homepage_search_win a{color: #63B8FF!important; text-decoration:none; cursor:pointer;}
    #homepage_search_win .hyperlinkSelectedStyle{color: #595959!important; text-decoration:none; cursor:default;}
    #homepage_search_win li{list-style-type:none;}
    #homepage_search_win .resultTableStyle{border-collapse:collapse; margin-right: 0px!important;}
    #homepage_search_win hr{border-top: 1px solid #F1F1F1; box-sizing: border-box;}
    #homepage_search_win .calendarStyle{margin-left: 12px; margin-top: 4px;}

    #hp_search_not_result_div li{list-style-type: disc; margin-left: 15px;}

    #hp_search_results_body_table .rightDivStyle{background-color: #F1F1F1; width: 165px; display: none;}

    #homepage_search_term_left p{margin-top: 10px; margin-left: 15px;}
    #homepage_search_term_left li{background-color: #F1F1F1; margin-top: 10px;}
    #homepage_search_term_left span{margin-left: 15px;}
    #homepage_search_term_left a{color: #595959!important; text-decoration:none; cursor:pointer; word-wrap:break-word;}
    #homepage_search_term_left .aSelectedStyle{color: #a6a6a6!important; text-decoration:none; cursor:default;}

    #homepage_search_term_right li{margin-top: 10px; margin-left: 10px;}
    #homepage_search_term_right a{color: #63B8FF!important; text-decoration:none; cursor:pointer;word-wrap:break-word;}
    #homepage_search_term_right span{margin-top: 10px; color: #595959!important; cursor:default;}

    #hp_search_advance_table input{width: 202px; margin-left: 10px;}
    #hp_search_advance_table td{padding-top: 10px;}
    /*kendo MultiSelect CSS*/
    #homepage_search_multi_select_div .k-multiselect-wrap li{background-color: #C3C3C3!important; height: 16px; line-height: 16px; width: auto;}
    #homepage_search_multi_select_div .k-delete{margin-top: -6px; margin-right: 3px;}
    #homepage_search_multi_select_div .k-multiselect-wrap input{height: 13px;}

    #homepage_search_results_table .firstTdStyle{text-align:center; padding: 10px 0px 0px 10px; width: 20px; border-bottom: 1px solid #f1f1f1;} 
    #homepage_search_results_table .titleStyle{font-size: 14px; color: #000000!important; padding-top: 10px; padding-left: 5px; word-wrap:break-word; padding-right: 5px; width: 356px;} 
    #homepage_search_results_table .tagsStyle{padding: 5px 0px 0px 5px; color: #a6a6a6;}
    #homepage_search_results_table .descStyle{padding: 5px 0px 0px 5px; width: 356px; text-overflow: ellipsis; word-break: break-word; color: #595959;}
    #homepage_search_results_table li{width: 333px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;}
    #homepage_search_results_table a{width: 333px; color: #a6a6a6!important; text-decoration:none; cursor:pointer; word-wrap:break-word;}
    #homepage_search_results_table .borderBottomStyle{padding: 5px 0px 0px 5px; color: #a6a6a6; border-bottom: 1px solid #f1f1f1; padding-bottom: 10px;word-break:break-all; width: 355px;}
</style>

<script src="js/smartstream.js"></script>

<div id="homepage_search_win" class="wp_fz_12">
    <div class="wp_ml_15 wp_mt_10 wp_mr_15 wp_mb_10">
        <div id="hp_search_header_simple_div">
            <input id="hp_search_key_input" type="text" class="wp_w_300 k-textbox" maxlength="200" />
            <button id="hp_search_simple_search_btn" class="k-button wp_w_50 wp_h_20 wp_ml_10" type="button">Search</button>
            <a id="hp_search_advance_a" href="#" class="wp_ml_20">Advanced Search</a>
            <div class="wp_mt_10">
                <a id="homepage_search_everything_a" href="#" class="hyperlinkSelectedStyle">Everything</a> |
                <a id="homepage_search_tags_a" href="#" class="wp_ml_10">Tags</a> |
            </div>
        </div>
        <div id="hp_search_header_advance_div" style="display: none">
            <li>Advanced Search</li>
            <table id="hp_search_advance_table" cellpadding="0" cellspacing="0">
                <tr>
                    <td>All These Words</td>
                    <td><input id="hp_search_advance_all" type="text" class="k-textbox" maxlength="50" /></td>
                    <td class="wp_pl_10">This Exact Word or Phrase</td>
                    <td><input id="hp_search_advance_exact" type="text" class="k-textbox" maxlength="50" /></td>
                </tr>
                <tr>
                    <td>Any of These Words</td>
                    <td><input id="hp_search_advance_any" type="text" class="k-textbox" maxlength="50" /></td>
                    <td class="wp_pl_10">None of These Words</td>
                    <td><input id="hp_search_advance_none" type="text" class="k-textbox" maxlength="50" /></td>
                </tr>
                <tr>
                    <td>Uploaded By</td>
                    <td><input id="hp_search_advance_uploaded" type="text" class="k-textbox" maxlength="50" /></td>
                    <td class="wp_pl_10">Created Date</td>
                    <td>
                        <div>
                            <input id="hp_search_datepicker_start" style="width: 88px;" maxlength=10 />
                             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;
                            <input id="hp_search_datepicker_end"  style="width: 88px; margin-left: 0px;" maxlength=10 />
                        </div>
                    </td>
                </tr>
            </table>
            <button id="hp_search_advance_back_btn" class="k-button wp_w_50 wp_h_20 wp_mt_10 btn-bf" style="margin-left: 577px;" type="button">Back</button>
            <button id="hp_search_advance_search_btn" class="k-button wp_w_50 wp_h_20 wp_mt_10" style="margin-left: 9px;" type="button">Search</button>
        </div>
    </div>
    <hr />
    <div id="hp_search_scroll" style="margin-bottom: 10px; width: 720px;">
        <table id="hp_search_results_body_table" class="resultTableStyle">
            <tr>
                <td id="hp_search_result_left_td" valign="top" style="display: none;">
                    <div id="homepage_search_term_left" class="wp_w_140 wp_mb_10">
                        <p>Community</p>
                        <input id="hp_search_comm_input" type="text" class="wp_ml_15 wp_w_115 wp_mt_5" maxlength="250" />
                        <li><span>Sources</span></li>
                        <div class="wp_mt_10 wp_ml_15">
                            <a id="hp_search_topic_refinement_all_a" href="#" class="aSelectedStyle">All</a>
                        </div> 
                        <div id="hp_search_topic_refinement_div"></div>
                        <br>
                        <li><span>Timelines</span></li>
                        <div class="wp_mt_10 wp_ml_15">
                            <a id="hp_search_date_refinement_all_a" href="#" class="aSelectedStyle">All</a>
                        </div> 
                        <div id="hp_search_date_refinement_div"></div>
                        <br>
                        <li><span>Uploaded By</span></li>
                        <div class="wp_mt_10 wp_ml_15">
                            <a id="hp_search_author_refinement_all_a" href="#" class="aSelectedStyle">All</a>
                        </div> 
                        <div id="hp_search_author_refinement_div"></div>
                        <div class="wp_ml_15 wp_mt_10" >
                        <a id="hp_search_show_more_a" href="#" style="color: #63b8ff!important;">Show more</a></div>
                    </div>
                </td>
                <td id="hp_search_result_middle_td" style="border-right: 1px solid #F1F1F1; border-left: 1px solid #F1F1F1;display: none;" class="wp_w_400" valign="top">
                    <div id="homepage_search_multi_select_div">
                        <select id="homepage_search_multi_select" style="width: 380px;" class="wp_mt_10"></select>
                    </div>
                    <li id="homepage_search_count_li" class="wp_ml_10 wp_mt_10">
                        <span id="hp_search_results_start_span"></span> - <span id="hp_search_results_end_span"></span> of about <span id="hp_search_results_count_span"></span> result(s)
                    </li>
                    <table id="homepage_search_results_table" cellspacing="0" class="wp_w_400" styel="padding-bottom: 10px;"></table>
                </td>
                <td valign="top" id="hp_search_not_result_td" class="wp_w_540">
                    <div id="hp_search_not_result_div" style="display: none; word-wrap: break-word;" class="wp_ml_15 wp_mt_10 wp_w_515">
                        <p id="hp_search_noresult_title_p" style="color:#000000;"></p>
                        <p class="wp_mt_20">
                            Suggestions:
                            <li>Ensure words are spelled correctly.</li>
                            <li>Try using synonyms or related searches.</li>
                            <li>Try removing search refinements or using more general keywords.</li>
                        </p>
                    </div>
                </td>
                <td valign="top">
                    <div id="homepage_search_term_right" class="rightDivStyle">
                        <li style="margin-top: 0px; padding-top: 10px;">Trending Search Results</li>
                        <div id="hp_search_trending_div"></div>
                        <br>
                        <li>Frequently Searched</li>
                        <div id="hp_search_frequent_div" style="padding-bottom: 10px;"></div>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>

<script type="text/x-kendo-template" id="hp_search_trending_template">
    <div class="wp_mt_10 wp_ml_10"><a href="\#">#: data.SearchKey #</a><span> (#: data.SCount #)</span></div>
</script>
<script type="text/x-kendo-template" id="hp_search_topic_refiners_template">
    <div id='hp_search_topic_refiners_#: data.id #' class="wp_mt_10 wp_ml_15"><a href="\#">#: data.TopicRefiners #</a></div>
</script>
<script type="text/x-kendo-template" id="hp_search_author_refiners_template">
    <div id='hp_search_author_refiners_#: data.id #' class="wp_mt_10 wp_ml_15"><a href="\#">#: data.AuthorRefiners #</a></div>
</script>
<script type="text/x-kendo-template" id="hp_search_date_refiners_template">
    <div id='hp_search_date_refiners_#: data.id #' class="wp_mt_10 wp_ml_15"><a href="\#">#: data.DateRefiners #</a></div>
</script>
<script type="text/x-kendo-template" id="hp_search_date_results_template">
    <tr>
        #var picTypes = ['jpeg','jpg','gif','bmp', 'png','tiff','tif'], fileTypes = ['pdf','docx','doc','pptx','ppt','xlsx','xls','txt','zip'], arr, typeIndex, fileIcon = ["imgs/pdfIcon20.png", "imgs/docIcon20.png", "imgs/docIcon20.png", "imgs/ppt20.png", "imgs/ppt20.png", "imgs/xlsIcon20.png", "imgs/xlsIcon20.png", "imgs/txt20.png", "imgs/zip20.png"], dataObj, notificationTargetId;#
        #if (data.EntityType == 'Document') {#
            #arr = data.TitleHyperLink.split('.');#
            #typeIndex = fileTypes.indexOf(arr[arr.length - 1]);#
            #if (typeIndex > -1) {#
                #dataObj = {type: fileTypes[typeIndex], src: fileIcon[typeIndex]};#
            #} else {#
                #dataObj = {type: picTypes[picTypes.indexOf(arr[arr.length - 1])], src: "imgs/img20.png"};#
            #}#
        #} else {#
            #if (data.NotificationType.toLowerCase() == 'c') { #
                # notificationTargetId = data.CommunityId; #
            # } else { #
                # notificationTargetId = data.TargetuserId; #
            #}#
        #}#
        <td rowspan="4" class="firstTdStyle" valign="top">
            #if (data.EntityType == 'Document') {#
            <!--    <a href="\#" onclick="FileViewer.load('#: data.TitleHyperLink #', '#: data.Title #', '#: dataObj.type #')">-->
                    <img src="#: dataObj.src #" onerror="this.src='imgs/20x20.png'" />
            <!--     </a>-->
            <!--    <span style="width: 20px; display: block; word-wrap:break-word;">(#: data.Size #)<span>-->
            #} else {#
                <img src="imgs/icons/main_documents_g_20_20.png" onerror="this.src='imgs/20x20.png'" />
            #}#
        </td>
        <td>
            #if (data.EntityType == 'Document') {#
                <a href="\#" onclick="FileViewer.load('#: data.TitleHyperLink #', '#: data.Title #', '#: dataObj.type #')" style="cursor:pointer;"><div id='hp_search_result_a_#: data.ToId #' class="titleStyle">#: data.Title #</div></a>
            #} else {#
                <a href="\#" onClick="homepageSearchWin.openStreamDetail('#: data.NotificationId #', '#: data.NotificationType #', '#: notificationTargetId #', '#: data.DateTime #')"><div id='hp_search_result_a_#: data.ToId #' class="titleStyle">#: data.Title #</div></a>
            #}#
        </td>
    </tr>
    <tr>
        <td class="tagsStyle">
            #var tags, len, tagsSepa = ',';#
            #if (data.Tags) {#
                #tags = data.Tags.split(tagsSepa);#
                #len = tags.length;#
                <img src="imgs/icons/tagicon.png" class="left wp_w_10 wp_h_10 wp_mt_4">
                #for (var i = 0; i < len; i++) {#
                    #if (i < len - 1) {#
                        #tags[i] = tags[i] + tagsSepa;#
                    #}#
                    <a class="middleResultsTags" href="\#">#: tags[i] #</a>
                #}#
            #}#
        </td>
    </tr>
    <tr>
        <td>
            #if (data.EntityType == 'Document') {#
                <a href="\#" onclick="FileViewer.load('#: data.TitleHyperLink #', '#: data.Title #', '#: dataObj.type #')" style="cursor:pointer;"><div class="descStyle">#: data.Description #</div></a>
            #} else {#
                <a href="\#" onClick="homepageSearchWin.openStreamDetail('#: data.NotificationId #', '#: data.NotificationType #', '#: notificationTargetId #', '#: data.DateTime #')"><div class="descStyle">#: data.Description #</div></a>
            #}#
        </td>
    </tr>
    <tr>
        <td>
            <div class="borderBottomStyle">Uploaded By: 
                <a href="\#" onclick="homepageProfileWin.user_info_win_create('#: data.FromId #')">#: data.Author #</a>
                     Date: #: data.DateTime.split(" ")[0] #
            </div>
        </td>
    </tr>
</script>

<script type="text/javascript">
    var homepageSearchWin = (function () {
        var winDiv = $('#homepage_search_win'), winTitle = "Search", everythingElem, tagsElem, multiSelect, mtDiv, trendingDiv, searchKeyInput, advanceA, headerSimpleDiv, headerAdvanceDiv, headerBackBtn, simpleSearchBtn, advanceSearchBtn, pn, resultLeftTd, resultMiddelTd, noResultTd, noResultDiv, authorRefinementDiv, dateRefinementDiv, scrollElem, maxHeight, scrollCreateFlag, frequentDiv, scrollHtml = winDiv.html(), commInput, ps, startSpan, endSpan, countSpan, mtItems, mtSelectedItems, mtElem, resultTable, trendingItems, frequentItems, searchTerm, searchType, searchRightDiv, scrollElemHeight, lastFlag, searchFlag, scrollFlag, commId, commInputAutoComplete, commIndex, kendoWindowElem, searchTagsFlag, startDatePicker, endDatePicker, advanceFlag, tagsSeparator = ",", topicRefinementDiv, showMoreAElem, authorData, leftAllAElem, commName, advanceTable, loader = new Workport.Loader(winDiv), loaderContent = {position:'center', imgColor:'black', textCSS:{'display': 'block', 'color':'black'}}, scrollDownLoaderContent = {position:'down', imgColor:'black', textCSS:{'display': 'none', 'color':'black'}}, authorRefinersTmpl = $("#hp_search_author_refiners_template"), trendingTmpl = $("#hp_search_trending_template"),  dateRefinersTmpl = $("#hp_search_date_refiners_template"), resultTmpl = $("#hp_search_date_results_template"), topicRefinementTmpl = $("#hp_search_topic_refiners_template"), noResultsTitleP, keywordsVal, middleTitle, browserType = Workport.getBrowser(), highlightKeys;
        var useragent = Workport.getBrowser();
        winDiv.kendoWindow({width:'720px', title: winTitle, modal: true, visible:false, resizable: false, draggable: false, actions: ["Close"], 
            close: function () {
                winDiv.empty();
                winDiv.html(scrollHtml);
            }
        });
        kendoWindowElem = winDiv.data("kendoWindow");

        function init () {
            everythingElem = $("#homepage_search_everything_a");
            tagsElem = $("#homepage_search_tags_a");
            multiSelect = $("#homepage_search_multi_select");
            mtDiv = $("#homepage_search_multi_select_div");
            trendingDiv = $("#hp_search_trending_div");
            searchKeyInput = $("#hp_search_key_input");
            advanceA = $("#hp_search_advance_a");
            headerSimpleDiv = $("#hp_search_header_simple_div");
            headerAdvanceDiv = $("#hp_search_header_advance_div");
            headerBackBtn = $("#hp_search_advance_back_btn");
            simpleSearchBtn = $("#hp_search_simple_search_btn");
            resultLeftTd = $("#hp_search_result_left_td");
            resultMiddelTd = $("#hp_search_result_middle_td");
            noResultTd = $("#hp_search_not_result_td");
            noResultDiv = $("#hp_search_not_result_div");
            topicRefinementDiv = $("#hp_search_topic_refinement_div");
            authorRefinementDiv = $("#hp_search_author_refinement_div");
            dateRefinementDiv = $("#hp_search_date_refinement_div");
            scrollElem = $("#hp_search_scroll");
            frequentDiv = $("#hp_search_frequent_div");
            commInput = $("#hp_search_comm_input");
            startSpan = $("#hp_search_results_start_span");
            endSpan = $("#hp_search_results_end_span");
            countSpan = $("#hp_search_results_count_span");
            resultTable = $("#homepage_search_results_table");
            searchRightDiv = $("#homepage_search_term_right");
            startDate = $("#hp_search_datepicker_start");
            endDate = $("#hp_search_datepicker_end");
            advanceAllInput = $("#hp_search_advance_all");
            advanceExactInput = $("#hp_search_advance_exact");
            advanceAnyInput = $("#hp_search_advance_any");
            advanceNoneInput = $("#hp_search_advance_none");
            advanceUploadedInput = $("#hp_search_advance_uploaded");
            advanceSearchBtn = $("#hp_search_advance_search_btn");
            showMoreAElem = $("#hp_search_show_more_a");
            leftAllAElem = [$("#hp_search_author_refinement_all_a"), $("#hp_search_date_refinement_all_a"), $("#hp_search_topic_refinement_all_a")];
            advanceTable = $("#hp_search_advance_table");
            middleTitle = $("#homepage_search_count_li");
            noResultsTitleP = $("#hp_search_noresult_title_p");

            ps = 10;
            scrollCreateFlag = false;
            searchFlag = false;
            scrollFlag = false;
            mtDiv.hide();

            startDatePicker = startDate.kendoDatePicker({format: "M/d/yyyy"}).data("kendoDatePicker");
            endDatePicker = endDate.kendoDatePicker({format: "M/d/yyyy"}).data("kendoDatePicker");
            startDate.attr("disabled", true);
            startDate.css("color", '#a6a6a6');
            endDate.attr("disabled", true);
            endDate.css("color", '#a6a6a6');
            $(".k-i-calendar").addClass("calendarStyle");
            $("#hp_search_datepicker_end").css("margin-left", "10px");
            advanceTable.find("span.k-i-calendar.calendarStyle.k-icon").attr("title", "Calender");

            multiSelect.kendoMultiSelect({
                animation: true,
                change: function(e) {
                    var val = this.value(), arr;
                    if (val.length == 0) {
                        setMultiSelectItmes(-1);
                        commInput.val("");
                    } else {
                        if ($.inArray(commName, val)) {
                            commName = "";
                            commId = "";
                            commIndex = -1;
                            commInput.val("");
                        }
                        arr=$.map(mtItems, function (_val) {
                            if ($.inArray(_val, val) == -1) {
                                return mtItems.indexOf(_val);
                            }
                        });
                        for (var i = 0; i < arr.length; i++) {
                            setMultiSelectItmes(arr[i], leftAllAElem[i]);
                        }
                    }
                    scrollFlag = true;
                    if(useragent != 'ipad'){
                        scrollElem.slimScroll({scrollTo: 0});
                    }
                    scrollFlag = false;
                    searchEvent();
                }
            });
            mtElem = multiSelect.data("kendoMultiSelect");
            mtDiv.find(".k-multiselect-wrap input").attr("disabled", "disabled");

            everythingElem.click(function () {
                if (!everythingElem.hasClass("hyperlinkSelectedStyle")) {
                    everythingElem.addClass("hyperlinkSelectedStyle");
                    tagsElem.removeClass("hyperlinkSelectedStyle");
                    searchTagsFlag = false;

                    if (searchKeyInput.val()) {
                        searchBtnClick();
                    }
                }
            });

            tagsElem.click(function () {
                if (!tagsElem.hasClass("hyperlinkSelectedStyle")) {
                    tagsElem.addClass("hyperlinkSelectedStyle");
                    everythingElem.removeClass("hyperlinkSelectedStyle");
                    searchTagsFlag = true;

                    if (searchKeyInput.val()) {
                        searchBtnClick();
                    }
                }
            });

            advanceA.click(function () {
                headerSimpleDiv.hide();
                headerAdvanceDiv.show();
                advanceFlag = true;
                searchKeyInput.val("");

                if (scrollCreateFlag) {
                    scrollElemHeight = scrollElemHeight - 92;
                    scrollElem.css("height", scrollElem.height() - 92);
                }
                loader.setContent(winDiv, loaderContent);

                noResultTd.show();
                noResultDiv.hide();
                resultLeftTd.hide();
                resultMiddelTd.hide();

                changeSearchBtnStatus();
            });

            headerBackBtn.click(function () {
                headerSimpleDiv.show();
                headerAdvanceDiv.hide();
                advanceFlag = false;

                advanceAllInput.val("");
                advanceExactInput.val("");
                advanceAnyInput.val("");
                advanceNoneInput.val("");
                advanceUploadedInput.val("");
                startDate.val("");
                endDate.val("");
                startDatePicker.value("");
                endDatePicker.value("");

                if (scrollCreateFlag) {
                    scrollElemHeight = scrollElemHeight + 92;
                    scrollElem.css("height", scrollElem.height() + 92);
                }
                loader.setContent(winDiv, loaderContent);

                noResultTd.show();
                noResultDiv.hide();
                resultLeftTd.hide();
                resultMiddelTd.hide();

                changeSearchBtnStatus();
            });

            showMoreAElem.click(function () {
                if (this.text == "Show more") {
                   if(useragent == 'ipad'){
                    showMoreAElem.html('Collapse');
                   }else{
                     this.text = "Collapse";
                   }

                    setLeftData(authorRefinementDiv, authorData, authorRefinersTmpl, 0);
                } else {
                    if(useragent == 'ipad'){
                        showMoreAElem.html('Show more');
                       }else{
                         this.text = "Show more";
                       }
                    
                    setLeftData(authorRefinementDiv, authorData.slice(0, 4), authorRefinersTmpl, 0);
                }
            });

            simpleSearchBtn.addClass("k-state-disabled");

            commInput.kendoAutoComplete({
                dataTextField: "name",
                filter: "contains",
                change: function(e) {
                    var currentItem = this.current();
                    if (currentItem) {
                        commIndex = currentItem.index();
                        commId = commInputAutoComplete.dataItem(commIndex).id;

                        commName = this.value();
                        setMultiSelectItmes(-2);
                        scrollFlag = true;
                        if(useragent != 'ipad'){
                            scrollElem.slimScroll({scrollTo: 0});
                        }
                        scrollFlag = false;

                        searchEvent();
                    }
                    this._old = '';
                }
            });
            commInput.parent().css('width', '115px');
            commInputAutoComplete = commInput.data("kendoAutoComplete");
            commInput.keyup (function () {
                if (!commInput.val()) {
                    commId = "";
                    commIndex = -1;
                    commName = "";
                }
            });
            maxHeight = Math.floor($(window).height() * 0.9) - 20;
            scrollElemHeight = maxHeight - 55;
            winDiv.css("height", maxHeight);
            winDiv.data("kendoWindow").center();

            if (browserType == 'safari_mac') {
                headerBackBtn.css('margin-left', '576px');
            } else if (browserType == 'ie10') {
                headerBackBtn.css('margin-left', '576px');
            } else if (browserType == 'ie11') {
                headerBackBtn.css('margin-left', '576px');
            }

            searchKeyInput.unbind().bind('keyup', changeSearchBtnStatus);
            advanceAllInput.unbind().bind('keyup', changeSearchBtnStatus);
            advanceExactInput.unbind().bind('keyup', changeSearchBtnStatus);
            advanceAnyInput.unbind().bind('keyup', changeSearchBtnStatus);
            advanceNoneInput.unbind().bind('keyup', changeSearchBtnStatus);
            advanceUploadedInput.unbind().bind('keyup', changeSearchBtnStatus);
            startDatePicker.unbind().bind("change", changeSearchBtnStatus);
            endDatePicker.unbind().bind("change", changeSearchBtnStatus);
        }

        function changeSearchBtnStatus (e) {
            if (advanceFlag) {
                if (e && e.keyCode == 13 && !advanceSearchBtn.hasClass("k-state-disabled")) {
                    advanceSearchBtn.click();
                } else {
                    if ((advanceAllInput.val() || advanceExactInput.val() || advanceAnyInput.val() || advanceNoneInput.val() || advanceUploadedInput.val() || startDate.val() || endDate.val()) && advanceSearchBtn.hasClass("k-state-disabled")) {
                        advanceSearchBtn.removeClass("k-state-disabled");
                        advanceSearchBtn.click(searchBtnClick);
                    } else if (!advanceAllInput.val() && !advanceExactInput.val() && !advanceAnyInput.val() && !advanceNoneInput.val() && !advanceUploadedInput.val() && !startDate.val() && !endDate.val() && !advanceSearchBtn.hasClass("k-state-disabled")) {
                        advanceSearchBtn.addClass("k-state-disabled");
                        advanceSearchBtn.unbind("click");
                    }
                }
            } else {
                if (e && e.keyCode == 13 && !simpleSearchBtn.hasClass("k-state-disabled")) {
                    simpleSearchBtn.click();
                } else {
                    if (searchKeyInput.val() && simpleSearchBtn.hasClass("k-state-disabled")) {
                        simpleSearchBtn.click(searchBtnClick);
                        simpleSearchBtn.removeClass("k-state-disabled");
                    } else if (!searchKeyInput.val() && !simpleSearchBtn.hasClass("k-state-disabled")) {
                        simpleSearchBtn.unbind("click");
                        simpleSearchBtn.addClass("k-state-disabled");
                    }
                }
            }
        }

        function searchBtnClick () {
            if (resultLeftTd.is(':hidden')) {
                mtItems = ["", "", ""];
                commIndex = 0;
                commId = "";
                commInput.val("");
            }
            scrollFlag = true;
            if(useragent !='ipad'){
                scrollElem.slimScroll({scrollTo: 0});
            }
            scrollFlag = false;

            searchEvent();
        }

        function searchEvent () {
            var resultsData, emptyFlag = false, checkCommName, aElems, count, startDateVal = "", endDateVal = "", allVal, exactVal, anyVal, noneVal, authorVal, searchKey;
            keywordsVal = '';
            highlightKeys = [];
            if (advanceFlag) {
                allVal = advanceAllInput.val();
                if (allVal) {
                    keywordsVal = allVal;
                    highlightKeys.push(allVal);
                    allVal = $.param({key: allVal}).substring(4);
                }
                exactVal = advanceExactInput.val();
                if (exactVal) {
                    highlightKeys.push(exactVal);
                    if (keywordsVal) {
                        keywordsVal = keywordsVal + ',' + exactVal;
                    } else {
                        keywordsVal = exactVal;
                    }
                    exactVal = $.param({key: exactVal}).substring(4);
                }
                anyVal = advanceAnyInput.val();
                if (anyVal) {
                    highlightKeys.push(anyVal);
                    if (keywordsVal) {
                        keywordsVal = keywordsVal + ',' + anyVal;
                    } else {
                        keywordsVal = anyVal;
                    }
                    anyVal = $.param({key: anyVal}).substring(4);
                }
                noneVal = advanceNoneInput.val();
                if (noneVal) {
                    highlightKeys.push(noneVal);
                    if (keywordsVal) {
                        keywordsVal = keywordsVal + ',' + noneVal;
                    } else {
                        keywordsVal = noneVal;
                    }
                    noneVal = $.param({key: noneVal}).substring(4);
                }
                authorVal = advanceUploadedInput.val();
                if (authorVal) {
                    highlightKeys.push(authorVal);
                    if (keywordsVal) {
                        keywordsVal = keywordsVal + ',' + authorVal;
                    } else {
                        keywordsVal = authorVal;
                    }
                    authorVal = $.param({key: authorVal}).substring(4);
                }

                searchTerm = {ps: 10, pn: 1, all: allVal, exact: exactVal, any: anyVal, none: noneVal, authorVal: authorVal, cid: commId};
                startDateVal = startDatePicker.value();
                if (startDateVal) {
                    startDateVal = new Date(startDateVal);
                    searchTerm.from = kendo.toString(startDateVal, "M/d/yyyy");
                    if (keywordsVal) {
                        keywordsVal = keywordsVal + ',' + searchTerm.from;
                    } else {
                        keywordsVal = searchTerm.from;
                    }
                } else {
                    searchTerm.from = "";
                }
                
                endDateVal = endDatePicker.value();
                if (endDateVal) {
                    endDateVal = new Date(endDateVal);
                    searchTerm.to = kendo.toString(endDateVal, "M/d/yyyy");
                    if (keywordsVal) {
                        keywordsVal = keywordsVal + ',' + searchTerm.to;
                    } else {
                        keywordsVal = searchTerm.to;
                    }
                } else {
                    searchTerm.to = "";
                }

                if (searchTerm.from && searchTerm.to) {
                    if (Date.parse(searchTerm.from,"MM/dd/yyyy") >= Date.parse(searchTerm.to,"MM/dd/yyyy")) {
                        Workport.utils.showTooltip("End date must be greater than start date.", $("#hp_search_datepicker_end").next().find("span.k-i-calendar").last());
                        return;
                    }
                }
                searchType = 5;

                if (mtItems[0] || mtItems[1] || mtItems[2]) {
                    searchTerm.refAuthor = mtItems[0];
                    searchTerm.refDate = mtItems[1];
                    searchTerm.refTopic = mtItems[2];
                    searchType = 6;
                }
            } else {
                keywordsVal = $.trim(searchKeyInput.val());
                highlightKeys.push(keywordsVal);
                searchKey = $.param({key: keywordsVal}).substring(4);
                if (!searchKey) {
                    Workport.utils.showTooltip("Please enter the keywords.", searchKeyInput);
                    return;
                }

                if (commName && (mtItems[0] || mtItems[1] || mtItems[2])) {
                    checkCommName = $.grep(WP_COMMUNITY_LIST, function (obj, i){return obj.name == commName});
                    if (checkCommName.length == 0) {
                        Workport.utils.showTooltip("Please input valid Community name.", commInput);
                        return;
                    }
                    searchTerm = {key: searchKey, ps: 10, pn: 1, cid: commId, refAuthor: mtItems[0], refDate: mtItems[1], refTopic: mtItems[2]};
                    searchType = 4;
                } else if (commName) {
                    checkCommName = $.grep(WP_COMMUNITY_LIST, function (obj, i){return obj.name == commName});
                    if (checkCommName.length == 0) {
                        Workport.utils.showTooltip("Please input valid Community name.", commInput);
                        return;
                    }
                    searchTerm = {key: searchKey, ps: 10, pn: 1, cid: commId};
                    searchType = 3;
                } else if (mtItems[0] || mtItems[1] || mtItems[2]) {
                    searchTerm = {key: searchKey, ps: 10, pn: 1, refAuthor: mtItems[0], refDate: mtItems[1], refTopic: mtItems[2]};
                    searchType = 2;
                } else {
                    searchTerm = {key: searchKey, ps: 10, pn: 1};
                    searchType = 1;
                }
            }
            loader.setOptions({position:'center', imgColor:'black', textCSS:{'display': 'block'}});
            
            getData();
        }

        function showData (resultsData) {
            if (resultsData) {
                searchFlag = true;
                resultTable.find("a").unbind("click");
                resultTable.empty();
                
                commInputAutoComplete.setDataSource(WP_COMMUNITY_LIST);
                if (resultLeftTd.is(':hidden')) {
                    noResultTd.hide();
                    noResultDiv.hide();
                    resultLeftTd.show();
                    resultMiddelTd.show();
                }
                setLeftData(topicRefinementDiv, resultsData.TopicRefinement, topicRefinementTmpl, 2);
                authorData = resultsData.AuthorRefinement;
                showMoreAElem.text("Show more");
                if (authorData.length < 5) {
                    showMoreAElem.hide();
                } else {
                    showMoreAElem.show();
                }
                setLeftData(authorRefinementDiv, authorData.slice(0, 4), authorRefinersTmpl, 0);
                setLeftData(dateRefinementDiv, resultsData.DateRefinement, dateRefinersTmpl, 1);

                setMiddleResults(resultsData);

                if (resultsData.ResultsOutput.length < 10) {lastFlag = true;}
            } else {
                searchFlag = false;
                if (noResultDiv.is(':hidden')) {
                    noResultTd.show();
                    noResultDiv.show();
                    resultLeftTd.hide();
                    resultMiddelTd.hide();
                    mtDiv.css("display", "none");
                }
                commId = "";
                commIndex = -1;
                mtItems = ["", "", ""];
                mtSelectedItems = ["", "", ""];
                if (advanceFlag) {
                    noResultsTitleP.text("We did not find any results for the search criteria.");
                } else {
                    noResultsTitleP.text("We did not find any results for " + keywordsVal);
                }
                commName = "";

                if (scrollCreateFlag) {
                    createScroll();
                }
            }
        }

        function setLeftData (_divElem, _data, _tmpl, _index) {
            var aElems, allFlag = true;

            _divElem.find("a").unbind("click");
            _divElem.empty();
            render(_data, _tmpl, _divElem);
            aElems = _divElem.find("a");
            aElems.click(_index, leftRefinementAClicked);
            if (mtSelectedItems[_index]) {
                for (var i = 0; i < aElems.length; i++) {
                    if (aElems[i].text == mtSelectedItems[_index]) {
                        setMultiSelectItmes(_index, $(aElems[i]));
                        leftAllAElem[_index].click(_index, leftRefinementAClicked);
                        _divElem.find("a:eq(" + i + ")").unbind("click");
                        allFlag = false;
                        break;
                    }
                }
                if (allFlag) {
                    leftAllAElem[_index].unbind("click");
                    setMultiSelectItmes(_index, leftAllAElem[_index], allFlag);
                }
            }
        }

        function createScroll () {
            var datas;
            scrollElem.unbind('slimscroll');
            if(useragent == 'ipad'){
                scrollElem.css('overflow-y','scroll');
                scrollElem.css('overflow-x','hidden');
                scrollElem.css('height',scrollElemHeight - 20);
                scrollElem.bind("scroll",function(){
                    if((scrollElem[0].scrollTop == 0) && !scrollFlag){ //top
                            scrollFlag = true;
                            lastFlag = false;
                            searchTerm.pn = 1;
                            loader.setContent(scrollElem, scrollDownLoaderContent);
                            loader.setPosition('up');
                            middleTitle.hide();
                            resultTable.empty();
                            getData();
                    } else if((scrollElem[0].scrollHeight <= scrollElem[0].scrollTop + scrollElem.height()) && !lastFlag && !scrollFlag ){ // bottom
                            scrollFlag = true;
                            loader.setContent(winDiv, scrollDownLoaderContent);
                            getData();
                    }
                });
            }else{
                scrollElem.slimScroll({wheelStep: 20, height: scrollElemHeight - 20, disableFadeOut: true});
                scrollElem.slimScroll().bind('slimscroll', function(e, pos) {
                    if (searchFlag) {   
                        if('bottom' == pos && !lastFlag && !scrollFlag){
                            scrollFlag = true;
                            loader.setContent(winDiv, scrollDownLoaderContent);
                            getData();
                        }
                        if ('top' == pos && !scrollFlag) {
                            scrollFlag = true;
                            lastFlag = false;
                            searchTerm.pn = 1;
                            loader.setContent(scrollElem, scrollDownLoaderContent);
                            loader.setPosition('up');
                            middleTitle.hide();
                            resultTable.empty();
                            getData();
                        }
                    }
                });
            }
            
            scrollCreateFlag = true;
        }

        function setMiddleResults (_data) {
            middleTitle.show();
            startSpan.text(searchTerm.pn * 10 - 9);
            var endCount = searchTerm.pn++ * 10, totalCount = _data.TotalResultsCount.TotalAvailableResults;
            if (endCount > totalCount) {
                endSpan.text(totalCount);
            } else {
                endSpan.text(endCount);
            }
            countSpan.text(totalCount);

            render(_data.ResultsOutput, resultTmpl, resultTable);

            createScroll();

            resultTable.find("a.middleResultsTags").click(function (e) {
                tagsElem.click();
                tagsAElemClick($(e.target).text().replace(tagsSeparator, ""));
            });
        }

        function tagsAElemClick (_val) {
            searchTagsFlag = true;
            searchKeyInput.val(_val);
            scrollFlag = true;
            if(useragent != 'ipad'){
                scrollElem.slimScroll({scrollTo: 0});
            }
            scrollFlag = false;
            searchEvent();
        }

        function leftRefinementAClicked (e) {
            e.preventDefault();
            $(e.target).unbind("click");
            setMultiSelectItmes(e.data, $(e.target));
            scrollFlag = true;
            if(useragent != 'ipad'){
                scrollElem.slimScroll({scrollTo: 0});
            }
            scrollFlag = false;

            searchEvent();
        }

        function setMultiSelectItmes (_type, _elem, _notFindFlag) {
            var items = [], index, val;

            if (_type == -1) {
                for (var i = 0; i < leftAllAElem.length; i++) {
                    leftAllAElem[i].addClass("aSelectedStyle");
                }
                dateRefinementDiv.find("a").removeClass("aSelectedStyle");
                authorRefinementDiv.find("a").removeClass("aSelectedStyle");
                topicRefinementDiv.find("a").removeClass("aSelectedStyle");
                mtItems = ["", "", ""];
                mtSelectedItems = ["", "", ""];
                commName = "";
            } else if (_type > -1) {
                leftAllAElem[_type].removeClass("aSelectedStyle");
                
                if (_type == 0) {
                    authorRefinementDiv.find("a").removeClass("aSelectedStyle");
                    leftAllAElem[_type].removeClass("aSelectedStyle");
                } else if (_type == 1) {
                    dateRefinementDiv.find("a").removeClass("aSelectedStyle");
                } else {
                    topicRefinementDiv.find("a").removeClass("aSelectedStyle");
                }
                _elem.addClass("aSelectedStyle");

                if (!_notFindFlag) {
                    val = _elem.text();
                    mtSelectedItems[_type] = val;
                    if (val == "All") {
                        mtItems[_type] = "";
                        mtSelectedItems[_type] = "";
                    } else {
                        index = val.indexOf("(");
                        index > -1 ? mtItems[_type] = val.substring(0, index) : mtItems[_type] = val;
                    }
                }

                for (var i = 0; i < mtItems.length; i++) {
                    if (mtItems[i]) {
                        items.push(mtItems[i]);
                    }
                }
            }
            if (commName) {
                items.push(commName);
            }
            
            if (items && items.length > 0) {
                mtElem.setDataSource(items);
                mtElem.value(items);
                if (mtDiv.is(':hidden')) {
                    mtDiv.show();
                }
            } else {
                mtDiv.css("display", "none");
            }
        }

        function getData () {
            if (!scrollFlag) {
                noResultTd.show();
                noResultDiv.hide();
                resultLeftTd.hide();
                resultMiddelTd.hide();
            }

            loader.show();
            if (advanceFlag) {
                if (searchType == 5) {
                    Workport.SearchAPI.searchAdvanced(callBack, searchTerm);
                } else if (searchType == 6) {
                    Workport.SearchAPI.searchAdvancedRefine(callBack, searchTerm);
                }
            } else {
                if (searchTagsFlag) {
                    if (searchType == 1) {
                        Workport.SearchAPI.searchTag(callBack, searchTerm);
                    } else if (searchType == 2) {
                        Workport.SearchAPI.searchTagRefine(callBack, searchTerm);
                    } else if (searchType == 3) {
                        Workport.SearchAPI.searchCommunityTag(callBack, searchTerm);
                    } else if (searchType == 4) {
                        Workport.SearchAPI.searchCommunityTagRefine(callBack, searchTerm);
                    }
                } else {
                    if (searchType == 1) {
                        Workport.SearchAPI.search(callBack, searchTerm);
                    } else if (searchType == 2) {
                        Workport.SearchAPI.searchRefine(callBack, searchTerm);
                    } else if (searchType == 3) {
                        Workport.SearchAPI.searchCommunity(callBack, searchTerm);
                    } else if (searchType == 4) {
                        Workport.SearchAPI.searchCommunityRefine(callBack, searchTerm);
                    }
                }
            }
        }

        function callBack (_responseData) {
            var datas, i;
            loader.hide();
            if (_responseData && _responseData.SearchOutput) {
                datas = _responseData.SearchOutput[0];
                for (i = 0; i < datas.AuthorRefinement.length; i++) {
                    datas.AuthorRefinement[i].id = i;
                }
                for (i = 0; i < datas.DateRefinement.length; i++) {
                    datas.DateRefinement[i].id = i;
                }
                for (i = 0; i < datas.TopicRefinement.length; i++) {
                    datas.TopicRefinement[i].id = i;
                }
                console.log(datas);
            }
            if (!scrollFlag) {
                showData(datas);
            } else {
                scrollFlag = false;
                if (datas) {
                    setMiddleResults(datas);
                    if (datas.ResultsOutput.length < 10) {lastFlag = true;}
                } else {
                    lastFlag = true;
                }
            }
        }

        function getTrendingFrequent (_tags) {
            loader.show();
            $.when(Workport.SearchAPI.searchTrending(), Workport.SearchAPI.searchFrequent()).done(function (_data1, _data2) {
                var data;
                searchRightDiv.find("a").unbind("click");
                if (_data1) {
                    data = _data1[0].SearchWidgetInfo;
                    if (data) {
                        render(data, trendingTmpl, trendingDiv);
                    }
                }
                if (_data2) {
                    data = _data2[0].SearchWidgetInfo;
                    if (data) {
                        render(data, trendingTmpl, frequentDiv);
                    }
                }
                searchRightDiv.find("a").click(rightAClick);
                createScroll();

                if (_tags) {
                    tagsAElemClick(_tags);
                }

                loader.hide();
                searchRightDiv.show();
            })
        }

        function rightAClick (e) {
            e.preventDefault();
            if (advanceFlag) {
                headerBackBtn.click();
            }
            
            searchKeyInput.val($(e.target).text());
            changeSearchBtnStatus();
            mtItems = ["", "", ""];
            commIndex = -1;
            commId = "";
            commInput.val("");

            scrollFlag = true;
            if(useragent !='ipad'){
                scrollElem.slimScroll({scrollTo: 0});
            }
            scrollFlag = false;

            searchEvent();
        }

        function render (_data, _tmplElem, _divElem) {
            var tmpl = kendo.template($.trim(_tmplElem.html())), arr = [];
            
            $.map(_data, function(_elem) {
                arr.push(tmpl(_elem));
            });
            $(arr.join('')).appendTo(_divElem);
            for (var index in highlightKeys) {
                $('#homepage_search_results_table').find("a.middleResultsTags").highlight(highlightKeys[index]);
                if (!searchTagsFlag) {
                    $('#homepage_search_results_table').find("div.titleStyle").highlight(highlightKeys[index]);
                    $('#homepage_search_results_table').find("div.descStyle").highlight(highlightKeys[index]);
                }
            }
        }

        return {
            open: function (_tags) {                
                lastFlag = false;
                searchTagsFlag = false;
                advanceFlag = false;
                commIndex = -1;
                mtItems = ["", "", ""];
                mtSelectedItems = ["", "", ""];
                keywordsVal = "";

                init();

                trendingItems = [];
                frequentItems = [];

                winDiv.data("kendoWindow").open();
                loader.setContent(winDiv, loaderContent);
                $("#homepage_search_win_wnd_title").next().find("span.k-i-close").attr("id","homepage_search_win_close");
                getTrendingFrequent(_tags);
            },
            openStreamDetail: function (notificationId, notificationType, targetId, createdDate) {
                var url;
                loader.show();
                Workport.StreamAPI.getStreamByNotify(function (data) {
                        if (data) {
                            if (data.notification) {
                                url = data.notification[0].EntityLinkUrl;
                            }
                        }
                    },{ notificationId: notificationId, type: notificationType, targetId: targetId, source: "globalsearch", sync:true});
                loader.hide();
                if (url) {
                    show_ss_topic_detail_win_with_iframe("globalsearch", url, notificationId, notificationType, targetId, createdDate, '', '', function () {});
                } else {
                    show_ss_topic_detail_win("globalsearch", notificationId, notificationType, targetId, createdDate, '', '', function () {});
                }
            }
        };
    })();

    $('#homepage_header_searchBtn').on({
        click:function(e){
            homepageSearchWin.open();
        }
    });
    /*$('#homepage_header_setBtn').on({
        click:function(e){
            var first;
            var $div1 = $("#inbox");
//            var $div2 = $("#boxes");
            var $div2 = $("#communities");
            var $temobj1 = $("<div></div>");
            var $temobj2 = $("<div></div>");
            $temobj1.insertBefore($div1);
            $temobj2.insertBefore($div2);
            $div1.insertAfter($temobj2);
            $div2.insertAfter($temobj1);
            $temobj1.remove();
            $temobj2.remove();
            var tempNO;
            tempNO = InboxNO;
//            InboxNO = BoxNO;
//            BoxNO = tempNO;
            InboxNO = CommunityNO;
            CommunityNO = tempNO;
            wp_main = $("#body_layout");
            HomePage_Scrollable.scroll(".item", wp_main);
//            HomePage_Scrollable.registerZoomCallback(BoxNO,boxes.switchMax,boxes.switchMin);
            HomePage_Scrollable.registerZoomCallback(CommunityNO,communities.switchMax,communities.switchMin);
            HomePage_Scrollable.registerZoomCallback(InboxNO,inbox.switchMax, inbox.switchMin);
            var objs = $(".item", wp_main);
            first = $(".item" + ':first', wp_main);
            for (var i = 0; i < objs.length; i++) {
                first[0] = objs[i];
                first.transition({rotateY: '0deg'});
            }
        }
    });*/
</script>